package com.yu.servlet;

import com.alibaba.fastjson.JSON;
import com.yu.Pojo.Goods;
import com.yu.service.GoodService;
import com.yu.serviceImpl.GoodServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/goods")
public class GoodListServlet extends HttpServlet {
    @Override
    protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        GoodService goodService = new GoodServiceImpl ();
        try {
            List<Goods> goodsList = new ArrayList<Goods> ();
            goodsList = goodService.findAllGoods ();
            if (goodsList != null)
            {
                String josnGoods = JSON.toJSONString (goodsList);


                //String rs = "sus!";
                PrintWriter out = resp.getWriter();
                out.print("goodLists:"+josnGoods);
                System.out.println (goodsList);
            }else {
                String rs = "失败!";
                PrintWriter out = resp.getWriter();
                out.print(rs);
                System.out.println ("访问失败！！！");
            }

        } catch (Exception e) {
            e.printStackTrace ();
        }
    }

    @Override
    protected void doPost (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }


}
